﻿using IRepository;
using IService;
using IService.SystemTableService;
using Model.Models;
using Model.Sys;
using ModelDto;
using ModelDto.Request.SysTableDto;
using ModelDto.Response.SysTableDto;
using SqlserverManager;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.SystemTableService
{
    [Service]
    public class PermissionService : BaseService<Permission>, IPermissionService
    {
        private readonly IDbConnectionFactory dbConnectionFactory;
        public PermissionService(IBaseRepository<Permission> BaseRepository,
            IDbConnectionFactory dbConnectionFactory) : base(BaseRepository)
        {
            this.dbConnectionFactory = dbConnectionFactory;
        }

        public List<PermissionResponseV2> GetPermissionListAll(PermissionRequestV1 request)
        {
            var dbcontext = dbConnectionFactory.CreateDbContext();
            var result = dbcontext.Queryable<Menu>().Where(f => f.Level == 2)
                .WhereIF(!string.IsNullOrEmpty(request.Id), f => f.ParentId == request.Id)
                .WhereIF(!string.IsNullOrEmpty(request.KeyWords), f => f.MenuName.Contains(request.KeyWords))
                .LeftJoin<Permission>((o, c) => o.Id == c.MenuId)
                    .Select((o, c) => new PermissionResponse
                    {
                        MenuId = o.Id,
                        MenuName = o.MenuName,
                        MenuCode = o.Code,
                        ParentId = o.ParentId,
                        Id = c.Id,
                        Code = c.Code,
                        Name = c.Name,
                        Sort = o.Sort
                    }).ToList();
            //能用但看着太费劲，重构
            //二级菜单下面嵌套一个权限List做前端的表单嵌套
            var permissionResponseV2 = result.GroupBy(f => new { f.MenuId, f.MenuName, f.MenuCode, f.Sort, f.ParentId }).Select(f => new PermissionResponseV2
            {
                MenuId = f.Key.MenuId,
                MenuName = f.Key.MenuName,
                MenuCode = f.Key.MenuCode,
                ParentId = f.Key.ParentId,
                Sort = f.Key.Sort,
                data = f.Select(k => k.Id).First() == null ? null : f.Select(k => new PermissionResponse_data
                {
                    Name = k.Name,
                    Id = k.Id,
                    Code = k.Code
                }).ToList()
            }).ToList();

            //var nodata = result.Where(f => string.IsNullOrEmpty(f.Id));
            //var youdata = result.Where(f => !string.IsNullOrEmpty(f.Id));
            //var respondyoudata = youdata.GroupBy(f => new { f.MenuId, f.MenuName, f.MenuCode, f.Sort }).Select(f => new PermissionResponseV2
            //{
            //    MenuId = f.Key.MenuId,
            //    MenuCode = f.Key.MenuCode,
            //    MenuName = f.Key.MenuName,
            //    Sort = f.Key.Sort,
            //    data = f.Select(k => new PermissionResponse_data
            //    {
            //        Name = k.Name,
            //        Id = k.Id,
            //        Code = k.Code,
            //    }).ToList()
            //}).ToList();
            //var respondnodata = nodata.GroupBy(f => new { f.MenuId, f.MenuName, f.MenuCode, f.Sort }).Select(f => new PermissionResponseV2
            //{
            //    MenuId = f.Key.MenuId,
            //    MenuCode = f.Key.MenuCode,
            //    MenuName = f.Key.MenuName,
            //    Sort = f.Key.Sort,
            //    data = null
            //}).ToList();
            //List<PermissionResponseV2> permissionResponseV2 = new List<PermissionResponseV2>();
            //permissionResponseV2.AddRange(respondyoudata);
            //permissionResponseV2.AddRange(respondnodata);
            permissionResponseV2 = permissionResponseV2.OrderBy(f => f.ParentId).ThenByDescending(f => f.Sort).ToList();
            return permissionResponseV2;

        }

        public List<PermissionDataV1> GetPermissionData(string roleId)
        {
            var dbcontext = dbConnectionFactory.CreateDbContext();
            //var permissionList = dbcontext.Queryable<Permission>().LeftJoin<RoleMenuAction>((o, c) => o.Id == c.PermissionId)
            //    .Where((o,c) => c.RooleId == roleId || string.IsNullOrEmpty(c.RooleId))
            //         .Select((o, c) => new PermissionDataV1
            //         {
            //             IsCheck = c.Id != null ? true : false,
            //             Id = o.Id,
            //             Name = o.Name,
            //             Code = o.Code,
            //             MenuId = o.MenuId,
            //         }).ToList();

            var roleMenuActionList = dbcontext.Queryable<RoleMenuAction>().Where(f => f.RooleId == roleId);

            var response = roleMenuActionList.RightJoin<Permission>((c, o) => o.Id == c.PermissionId)
                .Where((c, o) => c.RooleId == roleId || string.IsNullOrEmpty(c.RooleId))
                     .Select((c, o) => new PermissionDataV1
                     {
                         IsCheck = c.Id != null ? true : false,
                         Id = o.Id,
                         Name = o.Name,
                         Code = o.Code,
                         MenuId = o.MenuId,
                     }).ToList();





            return response;
        }
    }
}
